import { Language } from '@/language/data'
import {HumanDate, LanguageType} from "@/interface";

export const TEXT_MAP: Record<any, Record<any, any>> = {
    AM: {
        [Language.En]: 'am',
        [Language.Zh]: '上午'
    },
    PM: {
        [Language.En]: 'pm',
        [Language.Zh]: '下午'
    },
    AGO: {
        [Language.En]: ' ago',
        [Language.Zh]: '前'
    },
    JUST_NOW: {
        [Language.En]: 'Just now',
        [Language.Zh]: '刚刚'
    },
    MINUTES: {
        [Language.En]: 'minutes',
        [Language.Zh]: '分钟'
    },
    HOURS: {
        [Language.En]: 'hours',
        [Language.Zh]: '小时'
    },
    WEEKS: {
        [Language.En]: 'weeks',
        [Language.Zh]: '周'
    },
    DAYS: {
        [Language.En]: 'days',
        [Language.Zh]: '天'
    },
    MONTHS: {
        [Language.En]: 'months',
        [Language.Zh]: '个月'
    },
    YEARS: {
        [Language.En]: 'years',
        [Language.Zh]: '年'
    },
    WEEKDAYS: {
        [Language.En]: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
        [Language.Zh]: ['一', '二', '三', '四', '五', '六', '日']
    }
};

export const textHumanizer = (langData: Record<LanguageType, any>, language: LanguageType) => {
    return langData[language]
};

export const cloneDate = (date: Date) => {
    return new Date(date.getTime())
};

// Deconstruction timestamp date
export const dateToHuman = (date: Date) => {
    const week = date.getDay();
    return {
        day: date.getDate(),
        week: week === 0 ? 7 : week,
        month: date.getMonth() + 1,
        year: date.getFullYear()
    }
};

// HumanData -> date
export const humanToDate = (humanDate: HumanDate) => {
    const date = new Date();
    date.setDate(humanDate.day);
    date.setFullYear(humanDate.year);
    date.setMonth(humanDate.month - 1);
    return date
};

// HumanData -> like: 2016-01-02
export const humanDateToYMD = (humanDate: HumanDate, separator: string = '-') => {
    const formattedMonth = String(humanDate.month).padStart(2, '0');
    const formattedDay = String(humanDate.day).padStart(2, '0');
    return `${humanDate.year}${humanDate.month ? separator + formattedMonth : ''}${humanDate.day ? separator + formattedDay : ''}`;
};

export const isSameHumanDay = (target: HumanDate, target2: HumanDate) => {
    return (
        target.day === target2.day &&
        target.month === target2.month &&
        target.year === target2.year
    )
};

// Date -> like: 2016-01-02
export const dateToYMD = (date: Date) => {
    return humanDateToYMD(dateToHuman(date))
};

// YMD (2016-01-02) -> Date
export const ymdToDate = (date: string) => {
    const [year, month, day] = date.split('-').map(Number);
    const humanDate = { year, month, day, week: null };
    return humanToDate(humanDate)
};

// Date -> local date string
export const toLocalString = (date: Date) => {
    return date ? new Date(date).toLocaleString() : date
};

// 相对时间过滤器，传入时间，返回距离今天有多久
// @param _time (string | number | Date)
export const timeAgo = (_time: string | number | Date, language: LanguageType) => {
    const time = new Date(_time);
    const between = Date.now() / 1000 - (Number(time) / 1000);
    const hourS = 3600;
    const dayS = hourS * 24;
    const weekS = dayS * 7;
    const monthS = dayS * 30;
    const yearS = monthS * 12;

    const pluralize = (time: any, label: any) => {
        return `${time} ${label}${textHumanizer(TEXT_MAP.AGO, language)}`
    };

    if (between < hourS) {
        return ~~(between / 60) === 0
            ? textHumanizer(TEXT_MAP.JUST_NOW, language)
            : pluralize(~~(between / 60), textHumanizer(TEXT_MAP.MINUTES, language))
    }
    if (between < dayS) {
        return pluralize(~~(between / hourS), textHumanizer(TEXT_MAP.HOURS, language))
    }
    if (between < weekS) {
        return pluralize(~~(between / dayS), textHumanizer(TEXT_MAP.DAYS, language))
    }
    if (between < monthS) {
        return pluralize(~~(between / weekS), textHumanizer(TEXT_MAP.WEEKS, language))
    }
    if (between < yearS) {
        return pluralize(~~(between / monthS), textHumanizer(TEXT_MAP.MONTHS, language))
    }
    return pluralize(~~(between / yearS), textHumanizer(TEXT_MAP.YEARS, language))
};

// Date to YMD am/pm
export const humanizeYMD = (date: Date | number | string, language: LanguageType) => {
    if (!date) {
        return date
    }
    const newDate = new Date(date);
    const year = newDate.getFullYear();
    const month = newDate.getMonth() + 1;
    const day = newDate.getDate();
    const hour = newDate.getHours() > 11
        ? textHumanizer(TEXT_MAP.PM, language)
        : textHumanizer(TEXT_MAP.AM, language);
    return `${year}/${month}/${day} ${hour}`
};
